<template>
    <div class="welfare">
        <div v-if="welfareShow">
            <van-nav-bar :title="'AAT'+$t('benefit.title_one')" left-arrow fixed @click-left="_return" :z-index="666"></van-nav-bar>
            <div class="benefit-main">
                <div class="benefit-main-header">
                    <span class="num-item" v-if="listNum.toString().length < 8" v-for="(i, k) in (8 - listNum.toString().length)" >0</span>
                    <span class="num-item" v-for="(i, k) in listNum.toString().split('')" :key="k">{{ i }}</span>
                    <span style="margin-left: 5px;">{{ $t('benefit.remind_one') }}</span>
                </div>
                <div class="benefit-main-list">
                    <div class="benefit-main-item" v-for="(item, key) in dataList.list" :key="key" @click="lookDetail(item.id)">
                        <div class="benefit-main-item-header">
                            <div class="benefit-main-item-info">
                                <img :src="avatar" alt="" class="benefit-main-item-avatar">
                                <div class="benefit-main-item-info-desc">
                                    <p style="color: #333">{{ $t('benefit.name_one') }}</p>
                                    <p>{{ $t('benefit.remind_two') }} {{item.aready}}{{item.cointype.toUpperCase()}}</p>
                                </div>
                            </div>
                            <div class="benefit-main-item-status" v-if="item.status === 2">{{ $t('benefit.haven') }}</div>
                            <div class="benefit-main-item-status" v-if="item.status === 3">{{ $t('benefit.failed') }}</div>
                        </div>
                        <div class="benefit-main-item-content">
                            <div class="benefit-main-item-desc">
                                <h4 class="benefit-main-item-title">{{ item.title }}</h4>
                                <p class="benefit-main-item-desc-info">
                                    <img :src="sick" alt="">
                                    <span>{{ item.sickness }}</span>
                                </p>
                            </div>
                            <div class="benefit-main-item-cover">
                                <img :src="item.web +  item.img" alt="">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="benefit-main-footer">
                    <van-button class="benefit-main-footer-btn" @click="showKf">{{ $t('benefit.button_one') }}</van-button>
                </div>
            </div>
        </div>
        <detail v-if="detailShow" :child="child" @_return="_returns"></detail>
        <service v-if="kfShow" @_return="_returns"></service>
    </div>
</template>

<script>
    import detail from '@/components/welfare/detail'
    import service from '@/components/welfare/benefitKf'
    export default {
        data() {
            return {
                kfVisible: false,
                dataList:{
                    list:[]
                },
                avatar:require('../../assets/logos.jpg'),
                sick:require('../../assets/sick.png'),
                page:1,
                listNum:'',
                welfareShow:true,
                detailShow:false,
                child:'',
                kfShow:false,
            }
        },
        methods: {
            _return(){
                this.$emit('_return')
                window.removeEventListener("scroll", this.reachBottom,true)
            },
            showKf(){
                this.welfareShow = false
                this.kfShow = true
            },
            getData(){
                this.$api.get('welfare/index',{page:this.page})
                    .then(res =>{
                        if(res.detail.list.length < 10){
                            window.removeEventListener("scroll", this.reachBottom,true)
                        }
                        res.detail.list.forEach(item => this.dataList.list.push(item))
                        this.listNum = res.detail.listNum
                    })
            },
            getScrollTop () {
                // 考虑到浏览器版本兼容性问题，解析方式可能会不一样
                return document.documentElement.scrollTop || document.body.scrollTop
            },
            getWinHeight () {
                // 浏览器可见内容高度 || 浏览器所有内容高度(考虑到浏览器版本兼容性问题，解析方式可能会不一样)
                return document.documentElement.clientHeight || document.body.clientHeight
            },
            getScrollHeight() {
                let bodyScrollHeight = 0
                let documentScrollHeight = 0
                if (document.body) {
                    bodyScrollHeight = document.body.scrollHeight
                }
                if (document.documentElement) {
                    documentScrollHeight = document.documentElement.scrollHeight
                }
                // 当页面内容超出浏览器可视窗口大小时，Html的高度包含body高度+margin+padding+border所以html高度可能会大于body高度
                return (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight
            },
            isReachBottom () {
                const scrollTop = this.getScrollTop() // 获取滚动条的高度
                const winHeight = this.getWinHeight() // 一屏的高度
                const scrollHeight = this.getScrollHeight() // 获取文档总高度
                return scrollTop >= parseInt(scrollHeight) - winHeight
            },
            reachBottom() {
                if (this.isReachBottom()) {
                    if (this.listNum > 10) {
                        this.page ++
                        this.getData()
                    } else {
                        window.onscroll = null
                    }
                }
            },
            lookDetail(id){
                this.detailShow = true
                this.welfareShow = false
                this.child = id
                window.removeEventListener("scroll", this.reachBottom,true)
            },
            _returns(){
                this.detailShow = this.kfShow = false
                this.welfareShow = true
                this.getData()
                window.addEventListener("scroll", this.reachBottom,true)
            }
        },
        created() {
            this.getData()
        },
        mounted() {
            this.$emit('disappear')
            window.addEventListener("scroll", this.reachBottom,true)
        },
        components:{
            detail,
            service
        }

    }
</script>

<style lang="stylus" scoped>
    [class*=van-hairline]::after{
        border: none;
    }
    .benefit {
        &-main {
            padding: 0 0 50px;

            &-header {
                background:linear-gradient(180deg, #54d8b4, #26c196);
                height: 45px;
                line-height: 45px;
                color: #115f49;
                text-align: center;
                margin-top 50px
                .num-item {
                    display: inline-block;
                    width: 20px;
                    height: 25px;
                    line-height: 25px;
                    background-color: #fff;
                    color: #115f49;
                    font-size: 18px;
                    text-align: center;
                    margin-right: 2px;
                }
            }
            &-list {}
            &-item {
                width 87%
                margin 5px auto 0
                padding: 15px;
                border-radius 5px
                border-bottom 1px solid #ebebeb
                &-header {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                }
                &-avatar {
                    width: 40px;
                    height: 40px;
                    margin-right: 8px;
                }
                &-info {
                    display: flex;
                    &-desc {
                        p {
                            &:last-child {
                                font-size: 12px;
                                color: #2dc499;
                            }
                        }
                    }
                }
                &-status {
                    font-size: 14px;
                    color: #898989;
                }

                &-content {
                    margin-top: 16px;
                    display: flex;
                    justify-content: space-between;
                }
                &-desc {
                    margin-right: 15px;
                    flex: 1;
                    display: flex;
                    flex-flow: column nowrap;
                    justify-content: space-between;

                    &-info {
                        position: relative;
                        img {
                            vertical-align: middle;
                            margin-right: 4px;
                            width 14px
                        }
                        span {
                            vertical-align: middle;
                            font-size: 14px;
                            color: #f6543e;

                        }
                    }
                }
                &-title {
                    font-size: 16px;
                    color: #666;
                    line-height: 28px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                }
                &-cover {
                    width: 100px;
                    height: 100px;
                    img {
                        width: 100%;
                        height: 100%;
                    }
                }
            }

            &-footer {
                position: fixed;
                bottom: 0;
                left: 0;
                width: 100%;
                padding: 5px 10px;
                background #fff
                box-sizing: border-box;
                color #fff
                &-btn {
                    width: 100%;
                    border: none;
                    background:linear-gradient(180deg, #54d8b4, #26c196);;
                    color: #fff;
                }
            }
        }
    }
</style>